<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" :width="600">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="70px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="球拍型号" prop="racketModel" class="mr-15px">
            <el-input v-model="formData.racketModel" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="张数" prop="sheetNum">
            <el-input v-model="formData.sheetNum" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="轻重" prop="weight" class="mr-15px">
            <el-input v-model="formData.weight" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="裁剪尺寸" prop="cutSize">
            <el-input v-model="formData.cutSize" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="刀数" prop="knivesNum" class="mr-15px">
            <el-input v-model="formData.knivesNum" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
       <el-col :span="12">
          <el-form-item label="数量" prop="amount">
            <el-input v-model="formData.amount" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="裁剪理料" prop="materialsCut" class="mr-15px">
            <el-input v-model="formData.materialsCut" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
       <el-col :span="12">
          <el-form-item label="画线加层" prop="addLayers">
            <el-input v-model="formData.addLayers" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
  import * as materialPickApi from '@/api/hcxly/materialPick/Index'
  import { FormRules } from 'element-plus'

  defineOptions({ name: 'materialPickForm' });

  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗

  const router = useRouter() // 路由
  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formType = ref('') // 表单的类型：create - 新增；update - 修改
  const formData = ref({
    id: '',
    orderCode: '',
    type: '',
    code: '',
    specification: '',
    racketModel: '',
    sheetNum: '',
    weight: '',
    cutSize: '',
    knivesNum: '',
    amount: '',
    materialsCut: '',
    addLayers: '',
    materialStatus: ''
  })

  //打开弹窗
  const open = async (row) => {
    console.log(row)
    dialogVisible.value = true
    dialogTitle.value = t('完善信息')
    formData.value.id = row.id
    formData.value.orderCode = row.orderCode
    formData.value.type = row.type
    formData.value.code = row.code
    formData.value.materialStatus = row.materialStatus
    formData.value.racketModel = row.racketModel
    formData.value.sheetNum = row.sheetNum
    formData.value.weight = row.weight
    formData.value.cutSize = row.cutSize
    formData.value.knivesNum = row.knivesNum
    formData.value.amount = row.amount
    formData.value.materialsCut = row.materialsCut
    formData.value.addLayers = row.addLayers
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  //表单验证
  const formRules = reactive<FormRules>({
    // name: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
    // ip: [{ required: true, message: '轻重不能为空', trigger: 'blur' }],
    // port: [{ required: true, message: '裁剪尺寸不能为空', trigger: 'blur' }],
  })

  const formRef = ref() // 表单 Ref
  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value as unknown as materialPickApi.materialPickVO
      console.log(data)
      await materialPickApi.materialPickEdit(data)
      message.success(t('信息完善成功'))
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')
      resetForm()
    } finally {
      formLoading.value = false
    }
  }

  /** 重置表单 */
  const resetForm = () => {
    formData.value = {
      id: '',
      orderCode: '',
      type: '',
      code: '',
      specification: '',
      racketModel: '',
      sheetNum: '',
      weight: '',
      cutSize: '',
      knivesNum: '',
      amount: '',
      materialsCut: '',
      addLayers: '',
      materialStatus: ''
    }
    formRef.value?.resetFields()
  }
</script>
